<template>
	<view class="date-picker-filter" v-if="showDatePickerFilter">
		<view class="title">时间范围</view>
		<view class="select-box">
			<view v-for="(item,index) in dateList" :key="index"
				:class="['item',selectDate.label==item.label?'item-active':'']" @click="onItemSelect(item,index)">
				{{item.label}}</view>
		</view>
		<view class="bottom">
			<view class="bottom-button bottom-reset" @click="onReset">重置</view>
			<view class="bottom-button bottom-search" @click="onSearch">查询</view>
		</view>

	</view>
</template>
<script>
	import dayjs from 'dayjs';
	export default {
		name: 'DatePickerFilter',
		components: {
			scmDatePicker,
		},
		props: {
			value: {
				type: Object,
				default: {
					label: '',
					beginDate: '',
					endDate: '',
				},
			},
			showDatePickerFilter: {
				type: Boolean,
				default: false
			},
		},
		watch: {
			value(val) {
				this.$emit("update:value", val);
				this.selectDate = Object.assign({}, this.value);
			},
			showDatePickerFilter(val) {
				this.$emit('update:showDatePickerFilter', val);
			},
		},
		data() {
			return {
				dateList: [],
				selectDate: {
					label: '',
					beginDate: '',
					endDate: '',
				},
				scmDatePickerDate: {},
			}
		},
		mounted() {
			this.initData();
		},
		methods: {
			initData() {
				this.dateList = [{
						label: '近一周',
						beginDate: dayjs().subtract(7, 'day').startOf('day').valueOf(),
						endDate: dayjs().endOf('day').valueOf(),
					},
					{
						label: '近一月',
						beginDate: dayjs().subtract(1, 'month').startOf('day').valueOf(),
						endDate: dayjs().endOf('day').valueOf(),
					},
					{
						label: '近三月',
						beginDate: dayjs().subtract(3, 'month').startOf('day').valueOf(),
						endDate: dayjs().endOf('day').valueOf(),
					},
					{
						label: '自定义',
					}
				]
			},
			//选择
			onItemSelect(item, index) {
				if (item.label === '自定义') {
					this.selectDate = {
						label: item.label,
					};
					this.$emit('update:value', this.selectDate);
					this.$emit('change', this.selectDate);
				} else {
					this.selectDate = item;
					this.$emit('update:value', this.selectDate);
					this.$emit('change', this.selectDate);
				}
			},
			onChangeDate(val) {
				this.selectDate = {
					label: '自定义',
					beginDate: val.beginDate,
					endDate: val.endDate,
				}
				this.$emit('update:value', this.selectDate);
				this.$emit('change', this.selectDate);
			},
			onReset() {
				this.$emit('update:showDatePickerFilter', false);
				this.$emit('onReset');
			},
			onSearch() {
				this.$emit('update:showDatePickerFilter', false);
				this.$emit('onSearch');
			},
		},
	}
</script>
<style lang="scss" scoped>
	.date-picker-filter {
		width: 100vw;
		padding: 30rpx;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;

		.title {
			font-size: 28rpx;
			color: #303133;
			line-height: 40rpx;
		}

		.select-box {
			margin-top: 20rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-evenly;

			.item {
				background-color: #F0F2F5;
				height: 70rpx;
				line-height: 70rpx;
				padding: 0 36rpx;
				border-radius: 40rpx;
				font-size: 28rpx;
				color: #303133;
				text-align: center;

				&-active {
					background-color: #ecf1fc;
					color: #4876E8;
				}
			}
		}

		.scm-date-picker-box {
			margin-top: 20rpx;
		}

		.bottom {
			margin-top: 50rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-evenly;

			&-button {
				width: 310rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				border-radius: 10rpx;
			}

			&-reset {
				background-color: #FFFFFF;
				border: 1rpx solid #909399;
				font-size: 32rpx;
				color: #303133;
				font-weight: bold;
			}

			&-search {
				background-color: #4876E8;
				font-size: 32rpx;
				color: #FFFFFF;
				font-weight: bold;
			}
		}
	}
</style>